package org.ocean.online.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

public class CommonUtil {

	public static String getExtOfFile(String fileName){
		if(StringUtils.isBlank(fileName)){
			return "";
		}
		if(fileName.indexOf('.')==-1){
			return "";
		}
		String[] arr = fileName.split("\\.");
		return arr[arr.length-1];
	}
	
	public static String toString(InputStream ins) throws Exception{
		try{
			BufferedReader br = new BufferedReader(new InputStreamReader(ins,"utf-8"));
			StringBuffer sb = new StringBuffer("");  
//			br.readLine();
			String s = "";
			while ((s = br.readLine()) != null) { 
				sb.append(s);
			}
			br.close();
			return sb.toString();
		}catch(Exception ex){
			throw ex;
		}
	}
	static Map<String,String> types = new HashMap<String,String>();
	static{
		types.put("String", "java.lang.String");
		types.put("Integer", "java.lang.String");
		types.put("Long", "java.lang.Long");
		types.put("Float", "java.lang.Float");
		types.put("Double", "java.lang.Double");
		types.put("Date", "java.util.Date");
		types.put("Clob", "java.sql.Clob");
		types.put("Blob", "java.sql.Blob");
	}
	
	public static String convertType(String type){
		return types.get(type);
	}
	
	public static double getDirSize(File file) {     
        //判断文件是否存在     
        if (file.exists()) {     
            //如果是目录则递归计算其内容的总大小    
            if (file.isDirectory()) {     
                File[] children = file.listFiles();     
                double size = 0;     
                for (File f : children)     
                    size += getDirSize(f);     
                return size;     
            } else {//如果是文件则直接返回其大小,以“兆”为单位   
                double size = (double) file.length() / 1024 / 1024;        
                return size;     
            }     
        } else {     
            return 0.0;     
        }     
    }
}
